// =================================================================
// File: export.cpp
// Desc: Contains function for exporting files from the DPS file system
//	 to the host file system
// =================================================================

// =================================================================
// INCLUDES
// =================================================================

#include <string>
#include <fstream>
#include <iostream>
#include "FSManager.h"
#include "commands.h"
#include "Directory.h"
#include "File.h"
#include "Inode.h"

using namespace std;

// =================================================================
// FUNCTIONS
// =================================================================

// =================================================================
// Name: Export
// Desc: Exports a file on the DPS file system and stores it on the host machine
// Args: string src_path - the path of the src file in the DPS file system
//	 string dest_path - the path to put the file on the host machine
//       FSManager* fsm - pointer to active FSManager object
// =================================================================
void Export(string src_path, string dest_path, FSManager *fsm){

  ofstream outfile(dest_path.c_str(), ofstream::binary);

  int fd = open(src_path, "r", fsm);

  File *file_src = fsm->GetOpenFile(fd);
  Inode *node = fsm->ReadInode(file_src->GetInodeIndex());
  string data = read(fd, node->GetSize(), fsm);
  data += '\0';
  close (fd,fsm);
  
  // Write to file
  outfile.write(data.c_str(), data.length());

  outfile.close();


  cout << "Export of " << src_path << " to " << dest_path << " is a success!" << endl;
}

